﻿using System.Collections.Generic;
using System.Linq;
using VideoSeller.Business.Data;

namespace VideoSeller.Business.Models
{
	public class TagModel : Repository<Tag>
	{
		public Tag GetTag(string tagName)
		{
			tagName = tagName.Trim().ToLower();

			if (string.IsNullOrEmpty(tagName))
			{
				return null;
			}

			return Table.SingleOrDefault(x => x.Name == tagName) ?? Insert(new Tag { Name = tagName });
		}

		public void DeleteAll(IEnumerable<OnlineContentTag> items)
		{
			foreach (var item in items)
			{
				var oncat = DataContext.OnlineContentTags.SingleOrDefault(x => x.Id == item.Id);

				if (oncat != null)
				{
					DataContext.OnlineContentTags.DeleteOnSubmit(oncat);
				}
			}

			SubmitAllChanges();
		}
	}
}